<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Indexed DB Orm</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<script src="./../dist/idb.js"></script>
<script type="module">
  console.log(idb, idbTypes);
  const worker = new Worker('./worker.js', {
      type: "module"
  });

  import {Connector} from "../dist/es2015/connection/connector.js";
  import {Model} from "../dist/es2015/models/model.js";
  import {RelationTypes} from "../dist/es2015/models/model.interface.js";

  class Users extends Model {
      static TableName = 'users';

      constructor(db, table, connector) {
          super(db, table, connector);
      }

      userProfiles = () => {
          return this.hasOne(UserProfiles, 'userId', undefined, 'user_profiles').with([{
              model: 'users',
              foreignKey: '_id',
              type: RelationTypes.HasOne,
              localKey: 'userId',
              attributeName: 'user',
              func: (builder) => {
                  builder.with([{
                      model: 'addresses',
                      foreignKey: 'userIds',
                      type: RelationTypes.HasManyMultiEntry,
                      attributeName: 'addresses',
                  }]);
                  return builder;
              }
          }]);
      }
  }

  class UserProfiles extends Model {
      static TableName = 'userProfiles';

      constructor(db, table, connector) {
          super(db, table, connector);
      }
  }

  //
  // const model = new Model();
  // window.model = model;
  console.log(RelationTypes);
  const a = new Connector({
      tables: [{
          name: 'users',
          ormClass: Users,
          columns: [{
              name: 'email',
              attributes: {
                  unique: true
              }
          }]
      }, {
          name: 'userProfiles',
          columns: [{
              name: 'userId'
          }]
      }, {
          name: 'addresses',
          columns: [{
              name: 'userIds',
              attributes: {
                  multiEntry: true
              }
          }]
      }, {
          name: 'tasks',
          columns: [{
              name: 'userId',
          }]
      }],
      name: 'sample',
      version: 1
  });

  a.connect().then(async (models) => {
      console.log(a, models);
      self.models = models;

  });

  window.conn = a;

</script>
</body>
</html>
